#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

/// <summary>
/// GameConfig编辑器工具 - 用于创建和管理GameConfig配置文件
/// </summary>
public class GameConfigEditor
{
    private const string CONFIG_PATH = "Assets/Resources/Configs/GameConfig.asset";
    
    /// <summary>
    /// Unity编辑器启动时自动创建配置文件（如果不存在）
    /// </summary>
    [InitializeOnLoadMethod]
    public static void AutoCreateGameConfig()
    {
        // 延迟执行，避免在导入资源时执行
        EditorApplication.delayCall += () =>
        {
            if (!AssetDatabase.LoadAssetAtPath<GameConfig>(CONFIG_PATH))
            {
                CreateGameConfigInternal();
            }
        };
    }
    
    [MenuItem("GameConfig/创建GameConfig配置文件")]
    public static void CreateGameConfig()
    {
        CreateGameConfigInternal();
    }
    
    /// <summary>
    /// 内部创建配置文件的方法
    /// </summary>
    private static void CreateGameConfigInternal()
    {
        // 确保Resources/Configs目录存在
        string directory = "Assets/Resources/Configs";
        if (!AssetDatabase.IsValidFolder(directory))
        {
            // 创建目录
            if (!AssetDatabase.IsValidFolder("Assets/Resources"))
            {
                AssetDatabase.CreateFolder("Assets", "Resources");
            }
            AssetDatabase.CreateFolder("Assets/Resources", "Configs");
        }
        
        // 检查配置文件是否已存在
        GameConfig config = AssetDatabase.LoadAssetAtPath<GameConfig>(CONFIG_PATH);
        if (config != null)
        {
            if (EditorUtility.DisplayDialog("提示", $"配置文件已存在: {CONFIG_PATH}\n是否要重新创建？", "重新创建", "取消"))
            {
                AssetDatabase.DeleteAsset(CONFIG_PATH);
            }
            else
            {
                Selection.activeObject = config;
                return;
            }
        }
        
        // 创建新的配置文件
        config = ScriptableObject.CreateInstance<GameConfig>();
        AssetDatabase.CreateAsset(config, CONFIG_PATH);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        
        Debug.Log($"[GameConfigEditor] 已创建GameConfig配置文件: {CONFIG_PATH}");
        
        // 选中新创建的资源
        Selection.activeObject = config;
        EditorUtility.FocusProjectWindow();
        
        if (EditorUtility.DisplayDialog("成功", $"GameConfig配置文件已创建: {CONFIG_PATH}\n\n现在可以在Inspector中配置LogLevel等设置。", "确定"))
        {
            // 对话框关闭后自动选中
        }
    }
    
    [MenuItem("GameConfig/打开GameConfig配置文件")]
    public static void OpenGameConfig()
    {
        GameConfig config = Resources.Load<GameConfig>("Configs/GameConfig");
        if (config != null)
        {
            Selection.activeObject = config;
            EditorUtility.FocusProjectWindow();
        }
        else
        {
            // 尝试从Assets路径加载
            config = AssetDatabase.LoadAssetAtPath<GameConfig>(CONFIG_PATH);
            if (config != null)
            {
                Selection.activeObject = config;
                EditorUtility.FocusProjectWindow();
            }
            else
            {
                EditorUtility.DisplayDialog("错误", "未找到GameConfig配置文件，请先创建配置文件", "确定");
            }
        }
    }
}
#endif

